package com.party.admin.task;

import com.alibaba.fastjson.JSONObject;
import com.party.admin.biz.file.FileBizService;
import com.party.common.constant.Constant;
import com.party.common.redis.StringJedis;
import com.party.common.utils.StringUtils;
import com.party.core.model.member.ScanLoginOutput;
import com.party.core.service.cos.CosBizService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * @ClassName QuartzForScanLogin
 * @Description TODO
 * @Author yifeng
 * @Date 2019/2/20 9:49
 **/
@Component(value = "quartzForScanLogin")
public class QuartzForScanLogin {
    Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private StringJedis stringJedis;
    @Autowired
    private CosBizService cosBizService;
    @Autowired
    private FileBizService fileBizService;

    public void deleteInvalidData() {
        List<String> keys = stringJedis.getKeysByPrefix(Constant.SCAN_LOGIN + "*");
        for (String key : keys) {
            try {
                String value = stringJedis.getValue(key);
                if (StringUtils.isNotEmpty(value)) {
                    long currentTime = System.currentTimeMillis();
                    ScanLoginOutput scanLoginOutput = JSONObject.parseObject(value, ScanLoginOutput.class);
                    if (currentTime - scanLoginOutput.getCreateTime() >= 60000 * 30) {
                        stringJedis.delete(key);

                        String filePath = fileBizService.getUploadCIPath("loginQrCode", Constant.ADMIN_ID);
                        String deletePath = CosBizService.getDeleteQrCodePath(filePath, scanLoginOutput.getLgToken());
                        cosBizService.deleteFile(deletePath);
                    }
                }
            } catch (Exception e) {
                logger.error("刪除缓存中 扫码登录产生的 无效的值及对应腾讯云的图片", e);
            }
        }
    }
}
